915F - Imbalance Value of a Tree - CodeForces Solution


data structures dsu graphs trees *2400

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

long long n, mx, mn, u, v, w[1000007];
pair<long long, long long> a[1000007];
vector<long long> adj[1000007];

struct DSU{
    vector<int> par, sz;

    void make_set(int n){
        par.resize(n + 1);
        sz.resize(n + 1);
        for(int i = 1; i <= n; i++){
            par[i] = i;
            sz[i] = 1;
        }
    }

    int find_set(int v){
        if(par[v] == v) return v;
        return par[v] = find_set(par[v]);
    }

    bool union_sets(int u, int v){
        u = find_set(u);
        v = find_set(v);
        if(u == v) return false;
        if(sz[u] < sz[v]) swap(u, v);
        par[v] = u;
        sz[u] += sz[v];
        return true;
    }
} dsu;

void solveMax(){
    dsu.make_set(n);
    sort(a + 1, a + n + 1);
    for(int i = 1; i <= n; i++){
        int u = a[i].second;
        long long last = 0, szu = dsu.sz[dsu.find_set(u)];
        for(int j = 0; j < (int) adj[u].size(); j++){
            int v = adj[u][j];
            if(w[v] <= w[u]){
                long long szv = dsu.sz[dsu.find_set(v)];
                if(dsu.union_sets(u, v)){
                    mx += szu * szv * a[i].first;
                    mx += last * szv * a[i].first;
                    last += szv;
                }
            }
        }
    }
}

bool cmp(const pair<long long, long long> &a, const pair<long long, long long> &b){
    return a.first > b.first;
}

void solveMin(){
    dsu.make_set(n);
    sort(a + 1, a + n + 1, cmp);
    for(int i = 1; i <= n; i++){
        int u = a[i].second;
        long long last = 0, szu = dsu.sz[dsu.find_set(u)];
        for(int j = 0; j < (int) adj[u].size(); j++){
            int v = adj[u][j];
            if(w[v] >= w[u]){
                long long szv = dsu.sz[dsu.find_set(v)];
                if(dsu.union_sets(u, v)){
                    mn += szu * szv * a[i].first;
                    mn += last * szv * a[i].first;
                    last += szv;
                }
            }
        }
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    //freopen("marisa.inp", "r", stdin);
    //freopen("marisa.out", "w", stdout);
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i].first;
        a[i].second = i;
        w[i] = a[i].first;
    }
    for(int i = 1; i < n; i++){
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    solveMax();
    solveMin();

    cout << mx - mn << endl;
}


Comments

Submit
0 Comments
More Questions

1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals
1035. Uncrossed Lines
328. Odd Even Linked List
1219. Path with Maximum Gold
1268. Search Suggestions System
841. Keys and Rooms
152. Maximum Product Subarray
337. House Robber III